package app.server.project.main.service.controller;

import app.server.project.core.constants.Def;
import app.server.project.core.exception.SysError;
import app.server.project.core.http.RespModel;
import app.server.project.core.httpauth.UnAuth;
import app.server.project.core.token.TokenCache;
import app.server.project.core.util.AssertUtil;
import app.server.project.main.service.dto.recharge.TaskInfoDto;
import app.server.project.main.service.dto.task.TaskReq;
import app.server.project.main.service.service.task.TaskCfgService;
import app.server.project.main.service.service.task.TaskService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 任务模块
 */
@AllArgsConstructor
@RestController
@RequestMapping("/task")
public class TaskController {

    private TaskService taskService;
    private TaskCfgService taskCfgService;

    /**
     * 拉取任务列表
     *
     * @param
     */
    @PostMapping("/getTask")
    public RespModel<List<TaskInfoDto>> getTask(@RequestHeader(Def.UserId) long userId, @RequestHeader(Def.Lang) String lang) {
        return RespModel.success(this.taskService.getTask(userId, lang));
    }

    /**
     * 领取任务奖励
     *
     * @param userId
     * @param taskId
     */
    @PostMapping("/receive")
    public RespModel receive(@RequestHeader(Def.UserId) long userId, @RequestBody TaskReq req) {
        this.taskService.receive(userId, req.getTaskId());
        return RespModel.success();
    }

    /**
     * 变更任务状态
     *
     * @param userId
     * @param taskId
     */
    @PostMapping("/changeVerifyStatus")
    public RespModel changeVerifyStatus(@RequestHeader(Def.UserId) long userId, @RequestBody TaskReq req) {
        this.taskService.changeVerifyStatus(req, userId);
        return RespModel.success();
    }

    /**
     * 校验任务
     *
     * @param userId
     * @param taskId
     */
    @PostMapping("/verifyTask")
    public RespModel verifyTask(@RequestHeader(Def.UserId) long userId, @RequestBody TaskReq req) {
        this.taskService.verifyTask(req, userId);
        return RespModel.success();
    }

    /**
     * 上传客户端代码
     *
     * @param req
     */
    @UnAuth
    @PostMapping("/uploadImg/{userId}/{token}/{taskId}")
    public RespModel uploadGame(@RequestParam("Fdata") MultipartFile files, @PathVariable int taskId, @PathVariable long userId, @PathVariable String token) {
        AssertUtil.isTrue(TokenCache.chkToken(userId, token), SysError.ErrorToken);
        this.taskCfgService.uploadSingleImg(files, taskId);
        return RespModel.success();
    }
}
